{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "# '''\n",
    "# 异或\n",
    "# 0^0 = 0\n",
    "# 0^1 = 1\n",
    "# 1^0 = 1\n",
    "# 1^1 = 0\n",
    "# '''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.68849499]\n",
      " [ 0.44343329]\n",
      " [ 0.79666332]]\n"
     ]
    }
   ],
   "source": [
    "#输入数据\n",
    "X = np.array([[1,0,0],\n",
    "              [1,0,1],\n",
    "              [1,1,0],  \n",
    "              [1,1,1]])\n",
    "#标签\n",
    "Y = np.array([[-1],\n",
    "              [1],\n",
    "              [1],\n",
    "              [-1]])\n",
    "\n",
    "#权值初始化，3行1列，3是输入数，1是输出数，取值范围-1到1\n",
    "W = (np.random.random([3,1])-0.5)*2  # （0-1）--》-0.5（-0.5-0.5）--》*2（-1到1）\n",
    "\n",
    "print(W)\n",
    "# print(X.shape[0])  # 见下，答案是4,显示行数\n",
    "#学习率设置\n",
    "lr = 0.11\n",
    "#神经网络输出\n",
    "O = 0 # 这是O欧不是0零\n",
    "\n",
    "def update():\n",
    "    global X,Y,W,lr\n",
    "    O = np.sign(np.dot(X,W)) # shape:(3,1)，X和W都是矩阵，矩阵相乘，4行1列的数据\n",
    "    W_C = lr*(X.T.dot(Y-O))/int(X.shape[0])  #除以4就是求平均值，说明有4个数据\n",
    "    W = W + W_C # w+Δw,w的改变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "0\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "1\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "2\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "3\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "4\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "5\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "6\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "7\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "8\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "9\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "10\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "11\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "12\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "13\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "14\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "15\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "16\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "17\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "18\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "19\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "20\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "21\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "22\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "23\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "24\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "25\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "26\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "27\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "28\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "29\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "30\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "31\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "32\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "33\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "34\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "35\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "36\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "37\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "38\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "39\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "40\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "41\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "42\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "43\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "44\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "45\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "46\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "47\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "48\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "49\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "50\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "51\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "52\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "53\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "54\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "55\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "56\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "57\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "58\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "59\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "60\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "61\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "62\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "63\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "64\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "65\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "66\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "67\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "68\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "69\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "70\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "71\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "72\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "73\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "74\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "75\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "76\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "77\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "78\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "79\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "80\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "81\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "82\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "83\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "84\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "85\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "86\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "87\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "88\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "89\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "90\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "91\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "92\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "93\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "94\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "95\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "96\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "97\n",
      "[[-0.08349499]\n",
      " [ 0.00343329]\n",
      " [ 0.02666332]]\n",
      "98\n",
      "[[0.02650501]\n",
      " [0.05843329]\n",
      " [0.08166332]]\n",
      "99\n",
      "k= [-0.71553897]\n",
      "d= [-0.32456442]\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYAAAAD8CAYAAAB+UHOxAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAL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KW1YmjYpcbxqwUo7pcbYUZWa7A68Clzrnvgu6nlhzzv0KtAnPWY43sxznXEzmfpI+AKKiBWbaAAAA+ElEQVSxZWUyKe9608R6oGGJ1w2ADQHVIjFkZlXxv/wLnHOvBV1PPDnnvjGzWfi5n5gEQEq/BVSRLSslKS0AmplZEzOrBvQFJgZck0SZmRkwFljlnLs36Hriwcyyfnta0cx2AzoDH8ZqvJQOAMrYsjJVmdkpZrYe6ABMMrMpQdcUC+GJ/SHAFPzE4EvOuRXBVhVbZvYCMAdobmbrzWxg0DXFwZHA2cBx4f//Ljaz7kEXFWP7AzPNbCn+D51pzrk3YjWYVgKLiKSpVL8DEBGRMigARETSlAJARCRNKQBERNKUAkBEJE0pAERE0pQCQEQkTSkARETS1P8BjhQGwh3WzIUAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for i in range(100):\n",
    "    update() # 更新权值\n",
    "    print(W) # 打印当前权值\n",
    "    print(i) # 打印迭代次数\n",
    "    O = np.sign(np.dot(X,W))#计算当前输出  \n",
    "    if(O == Y).all(): #如果实际输出等于期望输出，模型收敛，循环结束\n",
    "        # .all当O和Y完全相等时才成立\n",
    "        print('Finished')\n",
    "        print('epoch:',i)\n",
    "        break\n",
    "\n",
    "#正样本\n",
    "x1 = [0,1]\n",
    "y1 = [1,0]\n",
    "#负样本\n",
    "x2 = [0,1]\n",
    "y2 = [0,1]\n",
    "\n",
    "#计算分界线的斜率以及截距\n",
    "k = -W[1]/W[2]\n",
    "d = -W[0]/W[2]\n",
    "print('k=',k)\n",
    "print('d=',d)\n",
    "\n",
    "xdata = (-2,3)\n",
    "\n",
    "plt.figure()\n",
    "plt.plot(xdata,xdata*k+d,'r')\n",
    "plt.scatter(x1,y1,c='b')\n",
    "plt.scatter(x2,y2,c='y')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
